package cn.itcast.service.system.impl;

import cn.itcast.dao.system.ModuleDao;
import cn.itcast.dao.system.UserDao;
import cn.itcast.domain.system.Module;
import cn.itcast.domain.system.User;
import cn.itcast.service.system.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private ModuleDao moduleDao;

    @Override
    public PageInfo<User> findPage(String companyId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
       List<User> list= userDao.findAll(companyId);
        return new PageInfo<User>(list,5);
    }

    @Override
    public void save(User user) {
        String password = user.getPassword();
        password=new Md5Hash(password,user.getEmail(),2).toString();
        user.setPassword(password);
        userDao.insert(user);
    }

    @Override
    public void update(User user) {
      userDao.updateById(user);
    }

    @Override
    public User findById(String id) {
        return userDao.findById(id);
    }

    @Override
    public void deleteById(String id) {
   //删除中间表信息
        userDao.deleteRoleAndUserId(id);
        userDao.deleteById(id);
    }

    @Override
    public List<User> findAll(String companyId) {
        return userDao.findAll(companyId);
    }

    @Override
    public List<String> findRoleIdsByUserId(String id) {
        return userDao.findRoleIdsByUserId(id);
    }

    @Override
    public void changeRole(String userid, String[] roleIds) {
        userDao.deleteRoleAndUserId(userid);
        for (String roleId : roleIds) {
            userDao.saveUserAndRole(userid,roleId);
        }
    }

    @Override
    public User findByEmail(String email) {
        return userDao.findByEmail(email);
    }





}
